
import { _decorator, Component, Node, EditBox } from 'cc';
const { ccclass, property } = _decorator;
import dwebAjax from "./dwebXHR"
/**
 * Predefined variables
 * Name = login
 * DateTime = Thu Feb 24 2022 20:16:25 GMT+0800 (中国标准时间)
 * Author = zxq6635624
 * FileBasename = login.ts
 * FileBasenameNoExtension = login
 * URL = db://assets/script/login.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */
 
@ccclass('login')
export class login extends Component {
    // [1]
    // dummy = '';

    // [2]
    // @property
    // serializableDummy = 0;

    username = null;
    password = null;
    chatBox = null

    start () {
        // [3]
        console.log('------------login start');
        this.username = this.node.getChildByName('UserName'); // 获取节点
        this.password = this.node.getChildByName('PassWord'); // 获取节点
        this.chatBox = this.node.getParent().getChildByName('chatBox'); // 获取节点
    }

    toLogin(event: Event, customEventData: string){
      console.log('------------点击登录');
      let username = this.username.getComponent(EditBox).string;
      let password = this.password.getComponent(EditBox).string;
      let data = {
        username: username,
        password: password
      }
      console.log(data);
      // A(async)j(js)a(and)x(xml) ajax
      dwebAjax.sendAjax("/api-json/login/",data,"POST",(res)=>{
        console.log(res)
        if (res == "pwd err" || res == "user none") {
          alert("用户名或密码错误");
          return
        }
        localStorage.setItem('token',res);
        this.node.active = false;
        this.chatBox.active = true;
      })
    }

    changeRegister(event: Event, customEventData: string){
      this.node.active = false
      this.node.getParent().getChildByName('Register').active = true
    }

    // update (deltaTime: number) {
    //     // [4]
    // }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
